<?php

/**
 * 自动生成 MerGoodsService
 * 
 * Apps/Common/Service/MerGoodsService.class.php
 * Zix @ 2015-06-15
 */

namespace Common\Service;

class MerGoodsService {

  private static $instance;

  public static function instance() {
    if (self::$instance == null) {
      $instance = new MerGoodsService();
    }

    return $instance;
  }

  function get_default_row() {
    return array(
      'mer_id' => '',
      'shop_type' => '',
      'price' => '',
      'ceiling_price' => '',
      'status' => '99',
      'create_date' => '',
      'release_date' => '',
      'shelf_date' => '',
      'jingle' => '',
      'brand_id' => '',
      'promotion_price' => '',
      'promotion_type' => '',
      'marketprice' => '',
      'storage_alarm' => '',
      'browse' => '',
      'salenum' => '',
      'collect' => '',
      'spec' => '',
      'storage' => '',
      'image' => '',
      'freight' => '',
      'vat' => '',
      'commend' => '',
      'good_star' => '',
      'comment_count' => '',
      'is_virtual' => '',
      'virtual_indate' => '',
      'virtual_limit' => '',
      'virtual_invalid_refund' => '',
    );
  }

  function get_by_id($id) {
    $MerGoods = M('MerGoods');
    $data = $MerGoods->find($id);
    return $data ? $data : array();
  }

  function get_by_cond($config) {
    $default = array(
      'page' => 1,
      'page_size' => 6,
      'status' => '',
      'count' => FALSE,
      'order' => 'DESC',
      'sort' => 'id',
    );

    $config = extend($config, $default);

    $MerGoods = M('MerGoods');

    $where = array();

    if (!empty($config['status'])) {
      $where['status'] = $config['status'];
    }

    if ($config['count']) {
      return $MerGoods->where($where)->count();
    } else {
      $order = $config['sort'] . ' ' . $config['order'];
      $limit = ($config['page'] - 1 ) * $config['page_size'] . ' , ' . $config['page_size'];
      $data = $MerGoods
          ->where($where)
          ->limit($limit)->order($order)->select();
      //echo $MerGoods->_sql();
      return $data ? $data : array();
    }
  }

  function create($data) {
    $MerGoods = M('MerGoods');
    $ret = $MerGoods->add($data);
    if ($ret) {
      return ajax_arr('添加成功', TRUE, array(
        'id' => $ret
      ));
    } else {
      return ajax_arr('添加失败', FALSE);
    }
  }

  function update($id, $data) {
    $MerGoods = M('MerGoods');

    $ret = $MerGoods->where("id = %d", $id)->save($data);
    if ($ret) {
      return ajax_arr('编辑成功', TRUE);
    } else {
      return ajax_arr('编辑失败', FALSE);
    }
  }

  function delete($ids) {
    $MerGoods = M('MerGoods');
    $ret = $MerGoods->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }

}
